using CfgTable;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_QuestPhase : UIBaseNode
    {
        #region Template Generate,don't modify

        protected class UIB_UIN_QuestPhase
        {
            #region ObjectBinding Generate

            public GameObject reach { protected set; get; }
            public ExText num { protected set; get; }
            public ExLoopVerticalScrollRect loopVerticalScrollRect { protected set; get; }
            public ExImage progressBar { protected set; get; }
            public GameObject left { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<GameObject>("reach", out var __tbv0);
                reach = __tbv0;
                __binding.TryGetVariableValue<ExText>("num", out var __tbv1);
                num = __tbv1;
                __binding.TryGetVariableValue<ExLoopVerticalScrollRect>("loopVerticalScrollRect", out var __tbv2);
                loopVerticalScrollRect = __tbv2;
                __binding.TryGetVariableValue<ExImage>("progressBar", out var __tbv3);
                progressBar = __tbv3;
                __binding.TryGetVariableValue<GameObject>("left", out var __tbv4);
                left = __tbv4;
            }


            #endregion ObjectBinding Generate
        }

        protected UIB_UIN_QuestPhase ui { get; set; }

        #endregion Template Generate,don't modify

        private IReadOnlyList<int> _rewards;
        private LoopScrollAdapter<UIN_OneItem> _rewardList;

        #region Override

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_QuestPhase();
            ui.InitBinding(csObjBind);
        }

        protected override void OnInit()
        {
            _rewardList = InitLoopScroll<UIN_OneItem>(ui.loopVerticalScrollRect, RefreshReward);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        protected override void OnDispose()
        {
        }

        #endregion

        #region Method

        public void Refresh(CfgQuestPhase phase, int curProgress, int prevProgress)
        {
            Show();
            _rewards = phase.Reward;
            _rewardList.RefillCells(_rewards.Count / 2);
            ui.num.text = phase.Progress.ToString();

            var reach = curProgress >= phase.Progress;
            ui.reach.SetActive(reach);
            ui.progressBar.fillAmount = (float)(curProgress - prevProgress) / (phase.Progress - prevProgress);

            ui.left.SetActive(phase.Phase == 1);
        }

        private void RefreshReward(UIN_OneItem item, int index)
        {
            var cidIndex = 2 * index;
            var numIndex = 2 * index + 1;
            var itemCid = _rewards[cidIndex];
            var itemNum = _rewards[numIndex];
            var cfgItem = TableCenter.item.Get(itemCid);
            item.Show(cfgItem);
            item.SetCount(itemNum, itemNum > 0);
        }

        #endregion
    }
}